/**
 * Created by John on 2017/9/12.
 */
var Excel = require('exceljs');

module.exports = function () {

    var workbook = new Excel.Workbook();
    workbook.creator = 'smtool';
    workbook.lastModifiedBy = 'smtool';
    workbook.created = new Date();
    workbook.modified = new Date();
    workbook.lastPrinted = new Date();
    workbook.views = [
        {
            x: 0, y: 0, width: 10000, height: 20000,
            firstSheet: 0, activeTab: 1, visibility: 'visible'
        }
    ];
    var sheetCount = 1;

    this.createSheet = function (name,columns,rows) {
        var worksheet = workbook.addWorksheet(name || 'Sheet'+sheetCount++);
        worksheet.columns = columns || [];
        worksheet.addRows(rows || []);
        return worksheet;
    }

    this.writeStream = function (stream,done){
        workbook.xlsx.write(stream)
            .then(function() {
                done && done();
            });
    }

    this.writeFile = function (uri,done){
        workbook.xlsx.writeFile(uri)
            .then(function() {
                done && done(uri);
            });
    }
}



